/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather.facebook;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tompai.datagather.facebook.FacebookResponse.Data;
import com.tompai.datagather.facebook.FacebookResponse.Data.From;
import com.tompai.datagather.facebook.FacebookResponse.Data.Shares;


/**
 * @date 2016年8月25日 下午7:07:59
 * @version
 * @since JDK 1.8
 */
public class FacebookAccountSearch {

	public static final Logger LOG = LoggerFactory.getLogger(FacebookAccountSearch.class);

	/**
	 * @param args
	 * @throws IOException
	 * @throws HttpException
	 * @since JDK 1.8
	 */
	
	public static void main(String[] args) throws HttpException, IOException {
		// TODO Auto-generated method stub

		//testFaceBookAccout();
		FacebookCrawler facebookSearch = new FacebookCrawler();
		String[] facebookAccouts=new String[]{"LeoMessi","BobMarley","Google",
				"BaiduMobileApps","rihanna","voachina"};
		int textLimits=100;
		//FacebookCrawler.saveFacebookAccount(, crawlerCount);
		//facebookSearch.search(facebookAccouts, textLimits);
	}
	
	public static void testFaceBookAccout(){
		
		FacebookResponse response = null;

		String base = "https://graph.facebook.com/v2.6/";
		String facebookAccount = "cnn";
		// String app_id = "1628625687408087";
		// String app_secret = "878b93ffea9985a0835dc9442756e05b";
		String fields = "id,created_time,from,link,message,picture,shares,type,updated_time";
		String count = "20";
		String app_id = "187807524965567";
		String app_secret = "b2cfeaa8a9e19cc3fb91253e3480fc76";
		String access_token = app_id + "%7C" + app_secret;
		String pageUrl = base + facebookAccount + "/posts/?fields=" + fields + "&limit=" + count + "&access_token=" + access_token;
		response = FacebookCrawler.executeSearchVps(pageUrl);
		List<Data> datas = response.getData();

		for (Data data : datas) {
			String post_id = data.getId();//帖子id
			String created_time = data.getCreated_time();//发表时间
			From from = data.getFrom();
			String userName = from.getName();//账号名称
			String userId = from.getId();//账号ID
			String link = data.getLink();//帖子链接
			String message = data.getMessage();//正文
	        String picture = data.getPicture();//图片链接
	        Shares shares = data.getShares();
	        Long sharesNumber = shares.getCount();//分享次数
	        String type = data.getType();//帖子类型
	        String updated_time = data.getUpdated_time();//帖子更新时间
	        System.out.println(post_id);
	        System.out.println(created_time);
	        System.out.println(userName);
	        System.out.println(userId);
	        System.out.println(link);
	        System.out.println(message);
	        System.out.println(picture);
	        System.out.println(sharesNumber);
	        System.out.println(type);
	        System.out.println(updated_time);
		}
		LOG.info(response.toString());
	}
}
